import { menu } from '../../../app/decorator';
const { ccclass, property } = cc._decorator;

@ccclass
@menu(__filename)
export default class PopAction extends cc.Component {
    @property
    private _begin = 0;
    @property
    get begin() { return this._begin; }
    set begin(v) { if (v >= 0 && v <= 1) this._begin = v; }

    @property
    private _end = 1;
    @property
    get end() { return this._end; }
    set end(v) { if (v >= 0 && v <= 1) this._end = v; }

    @property
    private _speed = 1;
    @property
    get speed() {
        if (this.begin == this.end) {
            return 0;
        } else if (this.begin > this.end) {
            return this._speed > 0 ? -this._speed : this._speed;
        } else {
            return this._speed >= 0 ? this._speed : -this._speed;
        }
    }
    set speed(v) { this._speed = v; }

    private action: cc.Tween = null;

    onEnable() {
        this.node.scale = this.begin;

        if (this.begin != this.end && this.speed) {
            const time = (this.end - this.begin) / this.speed;
            this.action = cc.tween(this.node)
                .to(time, { scale: this.end }, { easing: cc.easeBackOut().easing })
                .start();
        }
    }

    onDisable() {
        if (this.action) {
            this.action.stop();
            this.action = null;
        }
    }
}